package com.xiaoshuidi.cloud.module.rooms.job.xxljob;

import cn.hutool.core.collection.CollectionUtil;
import com.xiaoshuidi.cloud.framework.filings.config.FilingsProperties;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.InventoryCheckBo;
import com.xiaoshuidi.cloud.framework.filings.core.service.dto.InventoryRoomCheckBo;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.rooms.mq.event.InventoryCheckEvent;
import com.xiaoshuidi.cloud.module.rooms.pojo.dto.roomhouse.HouseDisperseFilingsDTO;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomHouseRepository;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 分散式房源备案 JOB
 *
 * @Author Feng
 * @date 2023/5/24
 */
@TenantJob
@Component
@RequiredArgsConstructor
public class DisperseFilingsJob {

    private final RoomHouseRepository roomHouseRepository;
    private final FilingsProperties filingsProperties;
    private final ApplicationEventPublisher publisher;

    @XxlJob("room-disperse-filings-job")
    public void execute(Integer days) {
        if (days == null || days <= 0) {
            days = 30;
        }
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now().minusDays(days), LocalTime.MIN);
        List<HouseDisperseFilingsDTO> dtos = roomHouseRepository.disperseFilingsList(startTime, null);
        if (CollectionUtil.isNotEmpty(dtos)) {
            //主房源信息
            List<HouseDisperseFilingsDTO> masterHouses = dtos.stream().filter(dto -> dto.getSuperId() == null || dto.getSuperId().equals(0L))
                    .collect(Collectors.toList());
            //合租房源信息
            Map<Long, List<HouseDisperseFilingsDTO>> salveHouseMap = dtos.stream().filter(dto -> dto.getSuperId() != null && dto.getSuperId().equals(0L))
                    .collect(Collectors.groupingBy(HouseDisperseFilingsDTO::getSuperId));
            masterHouses.forEach(house -> {
                // 推送备案信息 - 房源
                InventoryCheckBo bo = new InventoryCheckBo();
                bo.setUniqueDataId(house.getId().toString());
                bo.setSecretKey(filingsProperties.getSecretKey());
                bo.setOwnerName(house.getOwnerName());
                bo.setCardType(Integer.valueOf(house.getCardType()));
                bo.setCardNo(house.getCardNo());
                bo.setTelPhone(house.getTelPhone());
                bo.setPropertyNo(house.getPropertyNo());
                bo.setPropertyType(Integer.valueOf(house.getPropertyType()));
                bo.setRegion(house.getRegion())
                        .setHouseFloor(house.getHouseFloor())
                        .setStreetName(house.getStreetName())
                        .setItemName(house.getItemName())
                        .setBuildNo(house.getBuildNo() + "栋" + house.getUnitNo() + "单元")
                        .setRoomNo(house.getRoomNo())
                        .setHouseStructure(Integer.valueOf(house.getHouseStructure()))
                        .setHouseArea(house.getHouseArea())
                        // 房源户型 1-一户室，2-二户室，3-三户室，4-四户室，5-四户室以上
                        .setHouseType(house.getHouseType())
                        .setHouseOrientation(Integer.valueOf(house.getHouseOrientation()))
                        .setRenovation(Integer.valueOf(house.getRenovation()))
                        .setRegisterType(Integer.valueOf(house.getRegisterType()));
                bo.setHouseId(house.getExternalHouseId());
                List<HouseDisperseFilingsDTO> roomInfos = salveHouseMap.get(house.getId());
                InventoryRoomCheckBo roomCheckBo = null;
                if (CollectionUtil.isNotEmpty(roomInfos)) {
                    roomCheckBo = new InventoryRoomCheckBo();
                    roomCheckBo.setSecretKey(filingsProperties.getSecretKey());
                    roomCheckBo.setOperationType(1);
                    roomCheckBo.setUniqueDataId(house.getId().toString());
                    roomCheckBo.setRentalMode(2);
                    List<InventoryRoomCheckBo.HouseRoom> rooms = new ArrayList<>();
                    roomInfos.stream().forEach(roomInfo -> {
                        InventoryRoomCheckBo.HouseRoom houseRoom = new InventoryRoomCheckBo.HouseRoom().setRoomName(roomInfo.getRoomName()).setRoomArea(roomInfo.getHouseArea());
                        houseRoom.setHouseId(roomInfo.getExternalHouseId());
                        rooms.add(houseRoom);
                    });

                    roomCheckBo.setLeaseSubHouseSets(rooms);
                }
                publisher.publishEvent(new InventoryCheckEvent(this, bo, roomCheckBo, house.getTenantId()));
            });
        }
    }
}
